﻿using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace DeskNote
{
    static class InterceptKeys
    {
        private const int WH_KEYBOARD_LL = 13;
        private const int WM_KEYDOWN = 0x0100;
        private const int WM_KEYUP = 0x0101;
        private static LowLevelKeyboardProc _proc = HookCallback;
        private static int keyCount = 1;
        private static DeskNote.InsertNote inf = new DeskNote.InsertNote();
        private static bool shown;
        private static IntPtr _hookID = IntPtr.Zero;

        internal static void EnableHook()
        {
         
            _hookID = SetHook(_proc);
           // texpires.Enabled = true;
            texpires.Interval = 1000;
            texpires.Tick += new EventHandler(texpires_Tick);
        }

        static void texpires_Tick(object sender, EventArgs e)
        {
            keyCount = 1;
            texpires.Enabled = false;
        }


        internal static void ReleaseHook()
        {
          
           NativeMethods.UnhookWindowsHookEx(_hookID);
        }

        private static IntPtr SetHook(LowLevelKeyboardProc proc)
        {
            using (Process curProcess = Process.GetCurrentProcess())
            using (ProcessModule curModule = curProcess.MainModule)
            {
                return NativeMethods.SetWindowsHookEx(WH_KEYBOARD_LL, proc,
                   NativeMethods.GetModuleHandle(curModule.ModuleName), 0);
            }
        }

        internal delegate IntPtr LowLevelKeyboardProc(
            int nCode, IntPtr wParam, IntPtr lParam);

        private static IntPtr HookCallback(
            int nCode, IntPtr wParam, IntPtr lParam)
        {
            if (nCode >= 0 && wParam == (IntPtr)WM_KEYUP)
            {
                int vkCode = Marshal.ReadInt32(lParam);
                LaunchInsert((Keys)vkCode);
            }
            return NativeMethods.CallNextHookEx(_hookID, nCode, wParam, lParam);
        }

       
        
        private static void LaunchInsert(Keys ky)
        {
            if (shown && Keys.Enter==ky && inf.txtInsert.Focused)
            {
                ShowNoteTaker(false);
            }


            if (shown && Keys.Escape == ky)
            {
                ShowNoteTaker(false);
            }
            
            if ((ky == Keys.LControlKey || ky == Keys.RControlKey))
            {
                if (keyCount == 2)
                {
                    keyCount = 0;
                    if (!shown)
                    {
                        ShowNoteTaker(true);
                        texpires.Enabled = false;
                    }
                    else
                    {
                        ShowNoteTaker(false);
                    }
                }
                keyCount++;
                texpires.Enabled = true;
            }
            else
            {
                texpires.Enabled = true;
                keyCount = 1;
            }
        }

        private static void ShowNoteTaker(bool show)
        {
            if (!show)
            {
                shown = false;
                inf.Savedata();
                inf.WindowState = FormWindowState.Minimized;
                inf.Hide();
            }
            else
            {
                shown = true;
                inf.ResetNoteTaker();
                inf.Show();
                inf.WindowState = FormWindowState.Normal;
            }
        }

        static Timer texpires = new Timer();
        

    }
}